
fn main() {
    //string
    let mut s = String::from("hello");
    let s2 = s.clone();
    let mut s3 = s.clone();
    s.push_str(", world!");
    // println!("{}",s);
    let x =5;
    let y=x;
    // println!("{},world!,x={x},y={y}",s2);
    takes_ownership(s);
    makes_copy(y);

    // 引用 reference
    let len = calculate_length(&s2);
    println!("The length of '{}' is {}.",s2,len);

    // 借用 borrowing
    change(&mut s3);

    //悬垂指针
    let reference_to_nothing = dangle();
    println!("{}",reference_to_nothing);

    //slice
    let s4 = String::from("hello world");
    let _word = first_word(&s4);
    // s.clear();

}
fn takes_ownership(some_string: String){
    println!("{}",some_string);
}
fn makes_copy(some_integer: i32){
    println!("{}",some_integer);
}
fn calculate_length(s:&String)->usize{
    s.len()
}
fn change(some_string: &mut String){
    some_string.push_str(", world");
}
fn dangle()->String{
    let s=String::from("hello");
    s
}
fn first_word(s: &String) -> &str {
    let bytes= s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }

    &s[..]
}
